﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ERP_OutStoreEdit.aspx.cs" Inherits="CRM_Store_ERP_OutStoreEdit" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="../css/PageStyle.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../JavaScript/Common.js"></script>
    <title></title>
    <script type="text/javascript">
        function AfterOperate(SalesPlanId) {
            Ext.net.DirectMethods.SelectedSalesPlan(SalesPlanId);
        }

        function OnAdd_Click() {
            if (FieldEmptyCheck2Trim(formPanel)) {
                var grid = gridPanel_listdata;
                var rowIndex = grid.addRecord();
                grid.getView().focusRow(rowIndex);
                grid.startEditing(rowIndex, 1);
            }
        }

        function OnDel_Click() {
            var grid = gridPanel_listdata;
            if (!grid.hasSelection()) {
                Ext.Msg.alert("系统提示", "请选择一条记录!");
                return;
            }
            grid.deleteSelected();
            SetExtcombReadOnly();
        }

        function OnNUMKey(cellEditer, e) {
            var grid = gridPanel_listdata;
            if (e.getKey() == 13) {
                grid.store.commitChanges();
                var editor = cellEditer.gridEditor;
                var curCol = editor.col;
                var curRow = editor.row;
                var curItem = grid.store.data.items[curRow];
                var dataIndex = grid.colModel.columns[curCol].dataIndex;    //字段名
                var val = curItem.data[dataIndex];
                if (val == "") {
                    if (editor.getValue() != "")
                        val = editor.getValue();
                }
                var ret = EditNext(curCol, curRow, val);
                if (ret == 0) {
                    //变更总价，出库用到
                    var total = curItem.data["Supplyprice"] * editor.getValue();
                    curItem.set("TotalPrice", total);
                    grid.store.commitChanges();

                    var rowIndex = grid.addRecord();
                    grid.getView().focusRow(rowIndex);
                    EditNext(0, rowIndex, "");
                }
                if (ret == 1) {
                    grid.getView().focusRow(curRow);
                    return false;
                }
                return true;
            }
        }

        function EditNext(curCol, rowindex, val) {
            var grid = gridPanel_listdata;
            if (grid.colModel.columns[curCol].editor != null) {     //当前单元格内容不能为空
                if (val == "") {
                    return 1;
                }
            }
            for (i = curCol + 1; i < grid.colModel.columns.length - 1; i++) {    //跳转到下一个编辑框
                if (grid.colModel.columns[i].editor != null) {
                    grid.startEditing(rowindex, i);
                    return 2;
                }
            }
            return 0;
        }

        function SetExtcombReadOnly() {
            var b = gridPanel_listdata.store.data.length > 0;
            extcomb_CustomerId.setReadOnly(b);
            extcomb_StoreRoomId.setReadOnly(b);
        }

        function EmptyChk(obj) {
            if (obj.getValue() == "") {
                gridPanel_listdata.deleteRecord(obj.gridEditor.record);
                SetExtcombReadOnly();
            }
        }

        //物品名称中心库的模糊查询
        function SearchMatMoveByWhere(e) {
            var extcombname = extcomb_CURRENCYNAME;
            var searchText = "";
            if (e != null) {
                var key = e.getKey();
                if (extcombname.editable && (key == e.BACKSPACE || !e.isSpecialKey())) {
                    extcombname.lastKey = key;
                    extcombname.dqTask.delay(extcombname.queryDelay);
                    searchText = extcombname.getRawValue();
                    if (searchText != '' || key == e.BACKSPACE || key == 46) {
                        Ext.net.DirectMethods.BindCargoComb(searchText);
                        extcombname.selectedIndex = -1;
                    }
                }
            } else {
                searchText = extcombname.getSelectedItem().text;
                Ext.net.DirectMethods.BindCargoComb(searchText);
                extcombname.selectedIndex = -1;
            }
        }

        function Onextcomb_CURRENCYNAME_BeforeSelect() {
            if (extcomb_CURRENCYNAME.mimicing == false)
                return false;
        }

        function Onextcomb_CURRENCYNAME_select(item, record) {
            var grid = gridPanel_listdata;
            var row = item.gridEditor.row;
            var result = true;
            Ext.each(grid.getStore().getRange(), function (Gridrecord, rowIndex) {
                if (Gridrecord.data.ItemId == record.data.ItemId && row != rowIndex) {
                    result = false;
                }
            });
            if (result) {
                var rowRecord = grid.store.data.items[row];
                rowRecord.set('CargoId', record.data.CargoId);
                rowRecord.set('CargoName', record.data.CargoName); 
                rowRecord.set('ItemId', record.data.ItemId);
                rowRecord.set('BatchNo', record.data.BatchNo);
                rowRecord.set('CargoCode', record.data.CargoCode);
                rowRecord.set('Spec', record.data.Spec);
                rowRecord.set('Area', record.data.Area);
                rowRecord.set('Unit', record.data.Unit); 
                rowRecord.set('CusDetailId', record.data.CusDetailId);
                rowRecord.set('Count', 1);
                rowRecord.set('Supplyprice', record.data.Supplyprice);
                rowRecord.set('TotalPrice', record.data.Supplyprice);
                grid.store.commitChanges();
            } else {
                Ext.Msg.alert("系统提示", "该商品已存在!");
                grid.deleteRecord(grid.store.data.items[row]);
                return
            }
            SetExtcombReadOnly();
        }


        function SaveClick(type) {
            if (FieldEmptyCheck2Trim(formPanel)) {
                var datas = gridPanel_listdata.store.getRecordsValues();
                if (datas.length > 0) {
                    var strData = Ext.encode(datas);
                    Ext.net.DirectMethods.SaveData(strData, type);
                }
                else
                    Ext.Msg.alert("系统提示", "请至少选择一条商品!");
            }
        }

        function Onextcomb_Supplier_BeforeSelect() {
            if (extcomb_CustomerId.mimicing == false)
                return false;
        }
        function SearchOperate() {
            Store_Customer.reload();
        }
        function SearchOperate2() {
            if (extcomb_CustomerId.text == "") {
                Store_Customer.reload();
            }
        }

    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server" AjaxViewStateMode="Enabled" />
    <form id="form1" runat="server">
    <ext:Viewport ID="Viewport1" runat="server" Layout="FitLayout">
        <Content>
            <ext:Panel runat="server" Border="false" Layout="RowLayout">
                <TopBar>
                    <ext:Toolbar runat="server">
                        <Items>
                            <ext:Button ID="extBtn_Save" runat="server" Text="保存" Icon="Accept">
                                <Listeners>
                                    <Click Handler="SaveClick(0);" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="extBtn_SubmitInfo" runat="server" Text="提交" Icon="FolderGo">
                                <Listeners>
                                    <Click Handler="SaveClick(1);" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="extBtn_CheckInfo" runat="server" Text="审核" Icon="Lock">
                                <Listeners>
                                    <Click Handler="SaveClick(2);" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="extBtn_Exit" runat="server" Text="退出" Icon="Cancel">
                                <Listeners>
                                    <Click Fn="closeCurrTab" />
                                </Listeners>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill3" runat="server" />
                            <%--<ext:Button ID="extBtn_Print" runat="server" Text="打印" Icon="Printer">
                                <DirectEvents>
                                    <Click OnEvent="OnBtnPrintClick">
                                    </Click>
                                </DirectEvents>
                            </ext:Button>--%>
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Items>
                    <ext:Panel ID="formPanel" runat="server" Border="false" Padding="10" Height="72" Layout="ColumnLayout">
                        <Items>
                            <ext:Container runat="server" Layout="FormLayout" LabelAlign="Right" ColumnWidth="0.33">
                                <Items>
                                    <ext:TextField runat="server" ID="exttxtf_BoundNo" FieldLabel="出库单号" ReadOnly="true" AnchorHorizontal="0.95" />
                                    <ext:ComboBox ID="extcomb_StoreRoomId" runat="server" FieldLabel="申请库房"
                                        AnchorHorizontal="0.95" Editable="false" ValueField="STOREROOM_ID" DisplayField="STOREROOM_NAME"
                                        TabIndex="2">
                                        <Store>
                                            <ext:Store ID="Store_Rome" runat="server">
                                                <Reader>
                                                    <ext:JsonReader IDProperty="STOREROOM_ID">
                                                        <Fields>
                                                            <ext:RecordField Name="STOREROOM_ID" />
                                                            <ext:RecordField Name="STOREROOM_NAME" />
                                                        </Fields>
                                                    </ext:JsonReader>
                                                </Reader>
                                            </ext:Store>
                                        </Store>
                                    </ext:ComboBox>
                                    <ext:TextField runat="server" ID="exttxtf_Remark" FieldLabel="备注" MaxLength="400" AnchorHorizontal="0.95" />
                                    <ext:Hidden runat="server" ID="exthidn_SalesId" />
                                </Items>
                            </ext:Container>
                            <ext:Container runat="server" Layout="FormLayout" LabelAlign="Right" ColumnWidth="0.33">
                                <Items>
                                    <ext:TextField runat="server" ID="exttxtf_InUserName" FieldLabel="定制人" ReadOnly="true" AnchorHorizontal="0.95" />
                                    <ext:Hidden runat="server" ID="exthidn_InUser" />
                                    <ext:ComboBox runat="server" ID="extcomb_CustomerId" AllowBlank="false" FieldLabel="客户"
                                        DisplayField="CustomerName" ValueField="CustomerId" TypeAhead="false" LoadingText="Searching..."
                                        TriggerAction="All" ForceSelection="false" SelectOnFocus="true" EmptyText="请输入汉字或拼音简码"
                                        Mode="Remote" ItemSelector="div.search-item" MinChars="1" ListWidth="600" TabIndex="10"
                                        AnchorHorizontal="0.95" PageSize="10">
                                        <Template ID="Template1" runat="server">
                                            <Html>
                                                <table width="100%" style="background-color: #f0f0f0; font-size: small; font-weight: bold">
                                                    <tr>
                                                        <td style="width: 33%;">客户</td>
                                                        <td style="width: 33%;">拼音码</td>
                                                        <td style="width: 33%;">地址</td>
                                                    </tr>
                                                </table>
                                                <tpl for=".">
                                                    <div class="search-item">
                                                        <table width="100%">
                                                            <tr>
                                                                <td style="width:33%; ">{CustomerName}</td>
                                                                <td style="width:33%; ">{Spell}</td>
                                                                <td style="width:33%; ">{Address}</td>
                                                            </tr>
                                                        </table>
                                                    </div>
                                                </tpl>
                                            </Html>
                                        </Template>
                                        <Listeners>
                                            <Change Fn="SearchOperate" />
                                            <KeyDown Fn="SearchOperate2" />
                                            <Expand Fn="SearchOperate" />
                                            <BeforeSelect Fn="Onextcomb_Supplier_BeforeSelect" />
                                        </Listeners>
                                        <Store>
                                            <ext:Store runat="server" ID="Store_Customer" OnRefreshData="StoreCustomer_Refresh" RemotePaging="true">
                                                <Reader>
                                                    <ext:JsonReader IDProperty="CustomerId">
                                                        <Fields>
                                                            <ext:RecordField Name="CustomerId" />
                                                            <ext:RecordField Name="CustomerName" />
                                                            <ext:RecordField Name="Spell" />
                                                            <ext:RecordField Name="Address" />
                                                        </Fields>
                                                    </ext:JsonReader>
                                                </Reader>
                                            </ext:Store>
                                        </Store>
                                    </ext:ComboBox>
                                </Items>
                            </ext:Container>
                            <ext:Container runat="server" Layout="FormLayout" LabelAlign="Right" ColumnWidth="0.33">
                                <Items>
                                    <ext:TextField runat="server" ID="exttxtf_InDate" FieldLabel="定制时间" ReadOnly="true" AnchorHorizontal="0.95" />
                                </Items>
                            </ext:Container>
                        </Items>
                    </ext:Panel>
                    <ext:GridPanel runat="server" ID="gridPanel_listdata" RowHeight="1" BodyStyle="border-width:1 0 0 0;" AutoScroll="true" TrackMouseOver="true" StripeRows="true" ClicksToEdit="1">
                        <TopBar>
                            <ext:Toolbar runat="server">
                                <Items>
                                    <ext:ToolbarFill runat="server" />
                                    <ext:Button ID="extBtn_AddBook" runat="server" Text="加载销售计划" Icon="BookAdd" TabIndex="11">
                                        <DirectEvents>
                                            <Click OnEvent="extBtn_SalesPlan_Click" />
                                        </DirectEvents>
                                    </ext:Button>
                                    <ext:Button ID="extBtn_AddItem" runat="server" Text="添加" Icon="Add" TabIndex="12">
                                        <Listeners>
                                            <Click Fn="OnAdd_Click" />
                                        </Listeners>
                                    </ext:Button>
                                    <ext:Button ID="extBtn_DeleteItem" runat="server" Text="删除" Icon="Delete" TabIndex="12">
                                        <Listeners>
                                            <Click Fn="OnDel_Click" />
                                        </Listeners>
                                    </ext:Button>
                                </Items>
                            </ext:Toolbar>
                        </TopBar>
                        <ColumnModel>
                            <Columns>
                                <ext:RowNumbererColumn Header="<center>序号</center>" Width="35" />
                                <ext:Column Header="<center>商品名称</center>" Width="180" DataIndex="CargoName">
                                    <Editor>
                                        <ext:ComboBox runat="server" ID="extcomb_CURRENCYNAME" AllowBlank="false" DisplayField="CargoName"
                                            ValueField="CargoName" TypeAhead="false" LoadingText="Searching..." ForceSelection="true"
                                            TriggerAction="ALL" EmptyText="请输入汉字或拼音简码" Mode="Remote" ItemSelector="div.search-item"
                                            MinChars="1" ListWidth="900" TabIndex="1" PageSize="10">
                                            <Template ID="Template3" runat="server">
                                                <Html>
                                                    <table width="100%" style="background-color: #f0f0f0; font-size: small; font-weight: bold">
                                                <tr>
                                                    <td style="width: 16%;">
                                                        商品名称
                                                    </td>
                                                    <td style="width: 16%;">
                                                        商品代码
                                                    </td>
                                                    <td style="width: 16%;">
                                                        规格
                                                    </td>
                                                    <td style="width: 16%;">
                                                        批号
                                                    </td>
                                                    <td style="width: 10%;">
                                                        数量
                                                    </td>
                                                    <td style="width: 10%;">
                                                        采购价
                                                    </td>
                                                    <td style="width: 16%;">
                                                        产地
                                                    </td>
                                                </tr>
                                            </table>
                                                    <tpl for=".">
                                                <div class="search-item" >
                                                    <table width="96%;" >
                                                        <tr>
                                                            <td style="width:16%;" >{CargoName}</td>
                                                            <td style="width:16%;" >{CargoCode}</td>
                                                            <td style="width:16%;">{Spec}</td>
                                                            <td style="width:16%;" >{BatchNo}</td>
                                                            <td style="width:10%;" >{Count}</td>
                                                            <td style="width:10%;" >{Supplyprice}</td>
                                                            <td style="width:16%;" >{Area}</td>
                                                        </tr>
                                                    </table>
                                                </div>
                                            </tpl>
                                                </Html>
                                            </Template>
                                            <Listeners>
                                                <KeyUp Handler="SearchMatMoveByWhere(e);" />
                                                <Expand Handler="SearchMatMoveByWhere(null);" />
                                                <Select Fn="Onextcomb_CURRENCYNAME_select" />
                                                <BeforeSelect Fn="Onextcomb_CURRENCYNAME_BeforeSelect" />
                                                <SpecialKey Fn="OnNUMKey" />
                                                <Blur Fn="EmptyChk" />
                                            </Listeners>
                                            <Store>
                                                <ext:Store runat="server" ID="store_Cargo">
                                                    <Reader>
                                                        <ext:JsonReader IDProperty="ItemId">
                                                            <Fields>
                                                                <ext:RecordField Name="ItemId" Type="Int" />
                                                                <ext:RecordField Name="BatchNo" />
                                                                <ext:RecordField Name="Count" Type="Int" />
                                                                <ext:RecordField Name="ProduceDate" Type="Date" />
                                                                <ext:RecordField Name="DisinfectionDate" Type="Date" />
                                                                <ext:RecordField Name="ExpiryDate" Type="Date" />
                                                                <ext:RecordField Name="AsepticDate" Type="Date" />
                                                                <ext:RecordField Name="Supplyprice" />
                                                                <ext:RecordField Name="CargoName" />
                                                                <ext:RecordField Name="CargoCode" />
                                                                <ext:RecordField Name="Spec" />
                                                                <ext:RecordField Name="Spell" />
                                                                <ext:RecordField Name="Area" />
                                                                <ext:RecordField Name="Unit" />
                                                                <ext:RecordField Name="CusDetailId" />
                                                                <ext:RecordField Name="CusSupplyprice" />
                                                            </Fields>
                                                        </ext:JsonReader>
                                                    </Reader>
                                                </ext:Store>
                                            </Store>
                                        </ext:ComboBox>
                                    </Editor>
                                </ext:Column>
                                <ext:Column Header="<center>商品代码</center>" Width="120" DataIndex="CargoCode" />
                                <ext:Column Header="<center>规格</center>" Width="80" DataIndex="Spec" />
                                <ext:Column Header="<center>批号</center>" Width="120" DataIndex="BatchNo" />
                                <ext:Column Header="<center>数量</center>" Width="80" DataIndex="Count">
                                    <Editor>
                                        <ext:NumberField runat="server" ID="extnumf_NUMS" MinText="1" SelectOnFocus="true"
                                            DecimalPrecision="0">
                                            <Listeners>
                                                <SpecialKey Fn="OnNUMKey" />
                                            </Listeners>
                                        </ext:NumberField>
                                    </Editor>
                                </ext:Column>
                                <ext:Column Header="<center>采购价</center>" Width="120" DataIndex="Supplyprice" />
                                <ext:Column Header="<center>总计</center>" Width="120" DataIndex="TotalPrice" />
                                <ext:Column Header="<center>计量单位</center>" Width="80" DataIndex="Unit" />
                                <ext:Column Header="<center>产地</center>" Width="120" DataIndex="Area" />
                            </Columns>
                        </ColumnModel>
                        <Store>
                            <ext:Store runat="server" ID="Store1">
                                <Reader>
                                    <ext:JsonReader IDProperty="DetailId">
                                        <Fields>
                                            <ext:RecordField Name="DetailId" Type="Int" />
                                            <ext:RecordField Name="DBoundId" Type="Int" />
                                            <ext:RecordField Name="OrderId" Type="Int" />
                                            <ext:RecordField Name="ItemId" Type="Int" />
                                            <ext:RecordField Name="Count" Type="Int" />
                                            <ext:RecordField Name="CusDetailId" Type="Int" />
                                            <ext:RecordField Name="BatchNo" />
                                            <ext:RecordField Name="ProduceDate" Type="Date" />
                                            <ext:RecordField Name="DisinfectionDate" Type="Date" />
                                            <ext:RecordField Name="ExpiryDate" Type="Date" />
                                            <ext:RecordField Name="AsepticDate" Type="Date" />
                                            <ext:RecordField Name="StoreRoomId" Type="Int" />
                                            <ext:RecordField Name="StoreRoomName" />
                                            <ext:RecordField Name="Supplyprice" Type="Float" />
                                            <ext:RecordField Name="TotalPrice" Type="Float" />
                                            <ext:RecordField Name="CargoName" />
                                            <ext:RecordField Name="CargoCode" />
                                            <ext:RecordField Name="Spec" />
                                            <ext:RecordField Name="Area" />
                                            <ext:RecordField Name="ProduceName" />
                                            <ext:RecordField Name="Unit" />
                                            <ext:RecordField Name="Spell" />
                                            <ext:RecordField Name="CusSupplyprice" Type="Float" />
                                            <ext:RecordField Name="CusTotalPrice" Type="Float" />
                                            <ext:RecordField Name="CusCargoName" />
                                            <ext:RecordField Name="CusCargoCode" />
                                            <ext:RecordField Name="CusSpell" />
                                        </Fields>
                                    </ext:JsonReader>
                                </Reader>
                            </ext:Store>
                        </Store>
                        <SelectionModel>
                            <ext:RowSelectionModel runat="server" SingleSelect="true" />
                        </SelectionModel>
                        <BottomBar>
                            <ext:PagingToolbar runat="server" ID="PagingToolbar1" PageSize="20" DisplayInfo="true"
                                Height="25" DisplayMsg="显示{0} - {1},共{2}条" EmptyMsg="没有可显示的记录" StateID="Store1" />
                        </BottomBar>
                    </ext:GridPanel>
                </Items>
            </ext:Panel>
        </Content>
    </ext:Viewport>
    </form>
</body>
</html>
